package com.lemon.action;

import java.util.List;

import org.apache.struts2.dispatcher.HttpParameters;
import org.apache.struts2.dispatcher.Parameter;

import com.lemon.domain.Department;
import com.lemon.domain.Employee;
import com.lemon.service.DepartmentService;
import com.lemon.service.EmployeeService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class EmployeeAction extends ActionSupport implements ModelDriven<Employee> {

	private static final long serialVersionUID = 1L;
	private EmployeeService employeeService = new EmployeeService();
	private DepartmentService departmentService = new DepartmentService();
	// 查询所有部门
	private List<Department> departments = departmentService.list();
	// 模型驱动
	private Employee employee = new Employee();

	public String findEmployeesByDepartmentId() {
		ActionContext context = ActionContext.getContext();
		// 获取部门ID
		String param = getParam(context, "departmentId");
		if(param.equals("")) {
			param = "-1";
		}
		Long departmentId = Long.valueOf(param);
		// 根据部门ID查询员工
		List<Employee> employees = employeeService.findEmployeesByDepartmentId(departmentId);
		context.put("employees", employees);
		context.put("departments", departments);

		return SUCCESS;
	}

	/**
	 * 添加一个员工
	 * 
	 * @return
	 */
	public String save() {
		employeeService.save(employee);

		return SUCCESS;
	}

	/**
	 * 修改员工信息
	 * 
	 * @return
	 */
	public String edit() {
		employeeService.update(employee);
		
		return SUCCESS;
	}

	/**
	 * 删除员工
	 * 
	 * @return
	 */
	public String delete() {
		ActionContext context = ActionContext.getContext();
		// 获取要删除员工的ID
		String param = getParam(context, "id");
		Long id = Long.valueOf(param);
		employeeService.delete(id);

		return SUCCESS;
	}

	/**
	 * 跳转到修改页面
	 * 
	 * @return
	 */
	public String toEdit() {
		ActionContext context = ActionContext.getContext();
		String param = getParam(context, "id");
		Long id = Long.valueOf(param);

		// 被修改的员工
		Employee employee = employeeService.get(id);
		context.put("employee", employee);
		context.put("departments", departments);

		return SUCCESS;
	}

	/**
	 * 跳转到列表页面
	 * 
	 * @return
	 */
	public String toList() {
		ActionContext context = ActionContext.getContext();
		List<Employee> employees = employeeService.list();
		context.put("employees", employees);
		context.put("departments", departments);

		return SUCCESS;
	}

	/**
	 * 跳转到保存页面
	 * 
	 * @return
	 */
	public String toSave() {
		ActionContext context = ActionContext.getContext();
		context.put("departments", departments);

		return SUCCESS;
	}
	
	@Override
	public Employee getModel() {
		return employee;
	}

	private String getParam(ActionContext context, String key) {
		HttpParameters parameters = context.getParameters();
		Parameter param = parameters.get(key);
		return param.getValue();
	}

}
